package services;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Item;
import utils.WebUtils;
import core.Lookup;
import core.ManagerLocal;

public class DetailItem extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		
		process(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		process(req,resp);
	}

	private void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		Integer idItem = WebUtils.safeGetParameterInteger(req, "idItem");
		
		if(idItem == null){
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "idItem parameter is required");
			return;
		}		
		
		ManagerLocal manager = Lookup.lookupManager();
		
		Item item = manager.get(Item.class, idItem);
		
		if(item == null){
			resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "item not found");
			return;
		}
		
		
		String outputString = item.toString();
		
		PrintWriter writer = resp.getWriter();
		resp.setContentType("text/html;charset=UTF-8");
		writer.print(outputString);
		writer.close();
	}

}
